﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;


namespace HRM
{
    public partial class frmPositionDetailsInfo : Form
    {
        int _PDID;
        int _EmployeeID;
        int i = 0;
        PositionDetailInfo _PDInfo;
        DialogResult dlgResult;
        PositionDetailClient wcfPD;
        ParameterClient wcfParameter;
        RoleRightInfo listRole;
        GradeClient _wcfGrage;
        LevelClient _wcfLevel;
        DepartmentClient _wcfDepartment;
        BranchClient _wcfBranch;
        EmployeeInfo _EmployeeInfo;
        EmployeeClient _wcfEmployee;
        GradeDetailClient _wcfGradeDetail;
        bool isChange = false;
        string _EmployeeCode;
        int _GradeID = 0;
        int _LevelID = 0;
        public string EmployeeCode
        {
            get { return _EmployeeCode; }
            set { _EmployeeCode = value; }
        }

        public frmPositionDetailsInfo(int PDID, int EmployeeID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            dlgResult = DialogResult.Cancel;
            wcfPD = Service.PositionDetail();
            wcfParameter = Service.Parameter();
            _wcfGrage = Service.Grade();
            _wcfLevel = Service.Level();
            _wcfBranch = Service.Branch();
            _wcfDepartment = Service.Department();
            _wcfGradeDetail = Service.GradeDetail();
            _wcfEmployee = Service.Employee();
            _EmployeeID = EmployeeID;
            listRole = _listRole;
            _PDInfo = new PositionDetailInfo();
            UISetting.LoadBranch(ref cboBranch);
            UISetting.LoadPositionByBranchCode(ref cboPosition, Program.strBranchCode);
            UISetting.LoadInsType(ref cboInsType);
            UISetting.LoadGrade(ref cboGrade, Program.intBranchID);
            DateTextBox dtbFromPos = new DateTextBox(txtDate_BoNhiem);
            DateTextBox dtbToPos = new DateTextBox(txtDate_HetBoNhiem);
            DateTextBoxShort dtbFromDate = new DateTextBoxShort(txtDateStart);
            DateTextBoxShort dtbToDate = new DateTextBoxShort(txtDateEnd);
            DateTextBoxShort dtbSalayDate = new DateTextBoxShort(txtDateSalary);

            txtminSalary.Text = _wcfBranch.Branch_GetInfo(Program.intBranchID).MinBasicSalary.ToString("#,##0");
            
            _PDID = PDID;
            if (_PDID > 0)
            {
                _PDInfo = wcfPD.PositionDetail_GetInfoWithPDID(_PDID);
                Info2Form();
                //GetGradeAndLevel(_PDInfo.GradeID, _PDInfo.LevelID);
                grbMucCu.Enabled = false;
            }
            else
            {
                chkAcctive.Checked = true;
                grbMucCu.Enabled = true;
            }

            LoadOldData(_PDID);
            isChange = false;
        }

        public void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_Panel(ref panel2);
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
        //void GetGradeAndLevel(int GradeID, int LevelID)
        //{
        //    txtGrade.Tag = GradeID;
        //    if (GradeID > 0)
        //        txtGrade.Text = _wcfGrage.Grade_GetInfo(GradeID).Name;
        //    txtLevel.Tag = LevelID;
        //    if (LevelID > 0)
        //        txtLevel.Text = _wcfLevel.Level_GetInfo(LevelID).Name;
        //    DataTable dt = _wcfGradeDetail.GradeDetail_GetAmount(GradeID, LevelID).Tables[0];
        //    if (dt.Rows.Count > 0)
        //        i = Convert.ToInt32(dt.Rows[0]["GradeDetail_Month"]);
        //}
        void LoadOldData(int _PDID)
        {
            //-Đặt về Empty hết
            txtOldBasicSalary.Text = "";
            txtOldBasicFactor.Text = "";
            txtOldPositionFactor.Text = "";
            txtOldPositionAllowance.Text = "";
            txtOldOverFactor_Percent.Text = "";
            txtOldOverFactor.Text = "";
            txtOldOverAllowance.Text = "";
            txtOldJobFactor_Percent.Text = "";
            txtOldJobFactor.Text = "";
            txtOldJobAllowance.Text = "";
            txtOldAreaFactor.Text = "";
            txtOldAreaAllowance.Text = "";
            txtOldTelephone.Text = "";
            txtOldLunch.Text = "";
            txtOldOther.Text = "";
            txtOldTotal.Text = "";
            //

            DataTable dt = new DataTable();
            int n = 0;
            dt = wcfPD.PositionDetail_GetWithEmployee(_EmployeeID).Tables[0];
            n = dt.Rows.Count;

            if (n > 0)
            {
                int RowDataIndex = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    int PD_ID = Convert.ToInt32(dt.Rows[i]["PD_ID"]);
                    if (PD_ID == _PDID)
                    {
                        RowDataIndex = i;
                        break;
                    }
                }

                if (RowDataIndex == dt.Rows.Count - 1 && _PDID > 0)
                    RowDataIndex = RowDataIndex;
                else if (RowDataIndex == 0 && _PDID == 0)
                    RowDataIndex = RowDataIndex;
                else
                    RowDataIndex = RowDataIndex + 1;


                txtOldBasicSalary.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_BasicSalary"].ToString()).ToString("#,##0");
                txtOldBasicFactor.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_BasicFactor"].ToString()).ToString(UIParamater.Cham2So);
                txtOldPositionFactor.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_PositionFactor"].ToString()).ToString(UIParamater.Cham2So);
                txtOldPositionAllowance.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_PositionAllowance"].ToString()).ToString("#,##0");

                txtOldOverFactor.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_OverFactor"].ToString()).ToString(UIParamater.Cham3So);
                txtOldOverAllowance.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_OverAllowance"].ToString()).ToString(UIParamater.Cham2So);

                txtOldJobFactor.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_JobFactor"].ToString()).ToString(UIParamater.Cham3So);
                txtOldJobAllowance.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_JobAllowance"].ToString()).ToString(UIParamater.Cham2So);

                txtOldAreaFactor.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_AreaFactor"].ToString()).ToString(UIParamater.Cham2So);
                txtOldAreaAllowance.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_AreaAllowance"].ToString()).ToString(UIParamater.Cham2So);

                txtOldTelephone.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_TelephoneAllowance"].ToString()).ToString("#,##0");
                txtOldLunch.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_LunchAllowance"].ToString()).ToString("#,##0");
                txtOldOther.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_OtherAllowance"].ToString()).ToString("#,##0");
                txtOldTotal.Text = UISetting.GetDecimalNo(dt.Rows[RowDataIndex]["PD_TotalSalary"].ToString()).ToString("#,##0");
                //
                decimal OldBasicFactor = UISetting.GetDecimalNo(txtOldBasicFactor.Text);
                decimal OldPosionFactor = UISetting.GetDecimalNo(txtOldPositionFactor.Text);
                decimal OldOverFactor = UISetting.GetDecimalNo(txtOldOverFactor.Text);
                decimal OldJobFactor = UISetting.GetDecimalNo(txtOldJobFactor.Text);
                //
                if (OldBasicFactor != 0)
                {
                    decimal OldOverFactor_Percent = (OldOverFactor * 100 / OldBasicFactor);
                    txtOldOverFactor_Percent.Text = Math.Round(OldOverFactor_Percent).ToString();

                    if (OldBasicFactor + OldPosionFactor + OldOverFactor != 0)
                    {
                        decimal OldJobFactor_Percnet = (OldJobFactor * 100 / (OldBasicFactor + OldPosionFactor + OldOverFactor));
                        txtOldJobFactor_Percent.Text = Math.Round(OldJobFactor_Percnet).ToString();
                    }
                }
            }
        }

        private void Info2Form()
        {
            cboBranch.SelectedValue = _wcfDepartment.Department_GetInfo(_PDInfo.DepartmentID).BranchID;
            cboDepartment.SelectedValue = _PDInfo.DepartmentID;
            cboPosition.SelectedValue = _PDInfo.PositionID;
            txtDate_BoNhiem.Text = _PDInfo.DatePositionBegin == DateTime.MinValue ? "" : _PDInfo.DatePositionBegin.ToString("dd/MM/yyyy");
            txtDate_HetBoNhiem.Text = _PDInfo.DatePositionEnd == DateTime.MinValue ? "" : _PDInfo.DatePositionEnd.ToString("dd/MM/yyyy");
            cboGrade.SelectedValue = _PDInfo.GradeID;
            cboLevel.SelectedValue = _PDInfo.LevelID;
            txtJob.Text = _PDInfo.Job;
            txtDateStart.Text = _PDInfo.BeginDate == DateTime.MinValue ? "" : _PDInfo.BeginDate.ToString("MM/yyyy");
            txtDateEnd.Text = _PDInfo.EndDate == DateTime.MinValue ? "" : _PDInfo.EndDate.ToString("MM/yyyy");
            txtDateSalary.Text = _PDInfo.DateSalary == DateTime.MinValue ? "" : _PDInfo.DateSalary.ToString("MM/yyyy");
            txtminSalary.Text = _PDInfo.MinSalary.ToString("#,##0");
            chkAcctive.Checked = _PDInfo.IsActive;
            if (_PDInfo.IsActive)
                chkAcctive.Enabled = false;
            txtBasicSalary.Text = _PDInfo.BasicSalary.ToString(("#,##0"));
            txtBasicFactor.Text = _PDInfo.BasicFactor.ToString(UIParamater.Cham2So);

            txtPositionFactor.Text = _PDInfo.PositionFactor.ToString(UIParamater.Cham3So);
            txtPositionAllowance.Text = _PDInfo.PositionAllowance.ToString("#,##0");

            txtOverFactor.Text = _PDInfo.OverFactor.ToString(UIParamater.Cham3So);
            txtOverAllowance.Text = _PDInfo.OverAllowance.ToString("#,##0");

            txtJobFactor.Text = _PDInfo.JobFactor.ToString(UIParamater.Cham3So);
            txtJobAllowance.Text = _PDInfo.JobAllowance.ToString("#,##0");

            txtAreaFactor.Text = _PDInfo.AreaFactor.ToString(UIParamater.Cham3So);
            txtAreaAllowance.Text = _PDInfo.AreaAllowance.ToString("#,##0");

            txtTelephone.Text = _PDInfo.TelephoneAllowance.ToString("#,##0");
            txtLunch.Text = _PDInfo.LunchAllowance.ToString("#,##0");
            txtOther.Text = _PDInfo.OtherAllowance.ToString("#,##0");
            txtTotal.Text = _PDInfo.TotalSalary.ToString("#,##0");
            //chkTruyThu.Checked = _PDInfo.LaterInsurance;

            //Tính % phục cấp VK và phụ cấp nghề
            decimal BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            decimal PosionFactor = UISetting.GetDecimalNo(txtPositionFactor.Text);
            decimal OverFactor = UISetting.GetDecimalNo(txtOverFactor.Text);
            decimal JobFactor = UISetting.GetDecimalNo(txtJobFactor.Text);
            //
            if (BasicFactor != 0)
            {
                decimal OverFactor_Percent = (OverFactor * 100 / BasicFactor);
                txtOverFactor_Percent.Text = Math.Round(OverFactor_Percent).ToString();

                decimal JobFactor_Percnet = (JobFactor * 100 / (BasicFactor + PosionFactor + OverFactor));
                txtJobFactor_Percent.Text = Math.Round(JobFactor_Percnet).ToString();
            }
            

            
            

            isChange = false;
        }

        void Form2Info()
        {
            _PDInfo = new PositionDetailInfo();
            _PDInfo.ID = _PDID;
            _PDInfo.EmployeeID = _EmployeeID;
            _PDInfo.DepartmentID = Convert.ToInt32(cboDepartment.SelectedValue);
            _PDInfo.PositionID = Convert.ToInt32(cboPosition.SelectedValue);
            _PDInfo.DatePositionBegin = UISetting.GetDate(txtDate_BoNhiem.Text);
            _PDInfo.DatePositionEnd = UISetting.GetDate(txtDate_HetBoNhiem.Text);
            _PDInfo.GradeID = Convert.ToInt32(cboGrade.SelectedValue);
            _PDInfo.LevelID = Convert.ToInt32(cboLevel.SelectedValue);
            _PDInfo.BeginDate = UISetting.GetDate(txtDateStart.Text);
            _PDInfo.EndDate = UISetting.GetDate(txtDateEnd.Text);
            _PDInfo.DateSalary = UISetting.GetDate(txtDateSalary.Text);
            _PDInfo.IsActive = chkAcctive.Checked;            

            _PDInfo.MinSalary = UISetting.GetDecimalNo(txtminSalary.Text);
            
            _PDInfo.BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            _PDInfo.BasicSalary = UISetting.GetDecimalNo(txtBasicSalary.Text);
            
            _PDInfo.PositionFactor = UISetting.GetDecimalNo(txtPositionFactor.Text);
            _PDInfo.PositionAllowance = UISetting.GetDecimalNo(txtPositionAllowance.Text);

            _PDInfo.OverFactor = UISetting.GetDecimalNo(txtOverFactor.Text);
            _PDInfo.OverAllowance = UISetting.GetDecimalNo(txtOverAllowance.Text);
            
            //
            _PDInfo.JobFactor = UISetting.GetDecimalNo(txtJobFactor.Text);
            _PDInfo.JobAllowance = UISetting.GetDecimalNo(txtJobAllowance.Text);

            
            _PDInfo.AreaFactor = UISetting.GetDecimalNo(txtAreaFactor.Text);
            _PDInfo.AreaAllowance = UISetting.GetDecimalNo(txtAreaAllowance.Text);
            
            _PDInfo.TelephoneAllowance = UISetting.GetDecimalNo(txtTelephone.Text);
            _PDInfo.LunchAllowance = UISetting.GetDecimalNo(txtLunch.Text);
            _PDInfo.OtherAllowance = UISetting.GetDecimalNo(txtOther.Text);

            _PDInfo.TotalSalary = UISetting.GetDecimalNo(txtTotal.Text);
            _PDInfo.Description = "";
            _PDInfo.Job = txtJob.Text;

            //Update mã nhân viên:
            string _OldEmployeeCode = "";
            int _Number = 0;
            int _BranchID = 0;
            _BranchID = _wcfDepartment.Department_GetInfo(_PDInfo.DepartmentID).BranchID;
            EmployeeClient _wcfEmployee = Service.Employee();
            BranchClient wcfBranch = Service.Branch();
            if (_wcfEmployee.Employee_GetMaxEmployeeCode(_BranchID) != null)
            {
                _OldEmployeeCode = _wcfEmployee.Employee_GetMaxEmployeeCode(_BranchID).Code;
                if (_OldEmployeeCode.Trim().Length != 0)
                {
                    _Number = Convert.ToInt32(_OldEmployeeCode);
                    _Number = _Number + 1;
                }
                else
                    _Number = 1;
            }
            else
                _Number = 1;
            //_EmployeeCode = wcfBranch.Branch_GetInfo(_BranchID).Code + _Number.ToString("0000");
            _EmployeeCode = _Number.ToString("00000");
        }

        bool CheckValid()
        {
            if (Convert.ToInt32(cboDepartment.SelectedValue) == 0)
            {
                cboDepartment.Focus();
                MessageBox.Show(UpdateUI.GetString("chuachonphongban"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (Convert.ToInt32(cboPosition.SelectedValue) == 0)
            {
                cboPosition.Focus();
                MessageBox.Show("Chưa chọn chức vụ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            //if (txtGrade.Text.Length == 0)
            //{
            //    btnSelectGrade.Focus();
            //    MessageBox.Show("Bạn chưa chọn ngạch bậc lương!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    return false;
            //}
            if (UISetting.GetDate(txtDateStart.Text) == DateTime.MinValue)
            {
                txtDateStart.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            //if (_PDID == 0)
            //{
            //    _PDInfo = wcfPD.PositionDetail_GetInfo(_EmployeeID);
            //    if (_PDInfo != null)
            //    {
            //        if (DateTime.Compare(UISetting.GetDate(txtDateStart.Text), _PDInfo.BeginDate) < 0)
            //        {
            //            // Ngày bắt đầu phải lớn hơn ngày bắt đầu vao cty
            //            MessageBox.Show(UpdateUI.GetString("ngaybatdauphailonhonngaybatdaucu"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //            return false;
            //        }
            //    }
            //}

            if (_EmployeeID > 0)
            {
                _EmployeeInfo = _wcfEmployee.Employee_GetInfo(_EmployeeID);
                if (_EmployeeInfo != null)
                {
                    if (UISetting.GetDate(txtDateStart.Text).Month < _EmployeeInfo.StartDate.Month && UISetting.GetDate(txtDateStart.Text).Year <= _EmployeeInfo.StartDate.Year)
                    {
                        // Tháng bắt đầu nhận lương phải >= tháng vào công ty
                        txtDateStart.Focus();
                        MessageBox.Show(UpdateUI.GetString("ngaynhanluongphailonhonngayvaocongty"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return false;
                    }
                }

            }

            if (txtDateEnd.Text.Trim().Length > 4)
            {
                if (UISetting.GetDate(txtDateEnd.Text) == DateTime.MinValue)
                {
                    txtDateEnd.Focus();
                    MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return false;
                }
                else
                {
                    if (DateTime.Compare(UISetting.GetDate(txtDateEnd.Text), UISetting.GetDate(txtDateStart.Text)) < 0)
                    {
                        txtDateEnd.Focus();
                        MessageBox.Show(UpdateUI.GetString("ngayketthucphaimuonhonngaybatdau"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return false;
                    }
                }
            }
            if (!CheckTextValid.IsDecimal(txtBasicFactor.Text))
            {
                txtBasicFactor.Focus();
                MessageBox.Show("HS lương cơ bản không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtPositionFactor.Text))
            {
                txtPositionFactor.Focus();
                MessageBox.Show("HS chức vụ không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtOverFactor.Text))
            {
                txtOverFactor.Focus();
                MessageBox.Show("HS phụ cấp khác không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtBasicSalary.Text))
            {
                txtBasicSalary.Focus();
                MessageBox.Show("Tiền lương cơ bản không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtPositionAllowance.Text))
            {
                txtPositionAllowance.Focus();
                MessageBox.Show("Tiền phụ cấp không hợp lệ", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            //Sum();
            Form2Info();
            if (_PDID > 0)
            {
                if (wcfPD.PositionDetail_Update(_PDInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
            else
            {
                _PDInfo.ID = wcfPD.PositionDetail_Insert(_PDInfo);
                _PDID = _PDInfo.ID;

                #region Insert sang bên bảo hiểm
                //Kiểm tra có phải là nhân viên mới hay không ? Insert sang Bảo hiểm : Out
                //DataTable dt = wcfPD.PositionDetail_GetWithEmployee(_PDInfo.EmployeeID).Tables[0];
                //if (dt.Rows.Count == 0)
                //{
                //    HeaderClient wcfHeader = Service.Header();
                //    DetailClient wcfDetail = Service.Detail();
                //    HeaderInfo _infHeader = new HeaderInfo();
                //    DetailInfo _infDetail = new DetailInfo();
                //    RateClient _wcfRate = Service.Rate();
                //    RateInfo _infRate = new RateInfo();
                //    _infRate = _wcfRate.Rate_GetInfoByDate(_PDInfo.BeginDate);
                //    //Header:
                //    _infHeader.BranhCode = Program.strBranchBHXH;
                //    _infHeader.Date = DateTime.Now.Date;
                //    if (_PDInfo.BasicFactor > 0)
                //        _infHeader.Type = "HS";
                //    else
                //        _infHeader.Type = "ML";
                //    _infHeader.Period = 1;
                //    _infHeader.Month = DateTime.Now.Date.Month;
                //    _infHeader.Year = DateTime.Now.Date.Year;
                //    _infHeader.IsInitial = false;
                //    _infHeader.BasicSalary = _PDInfo.MinSalary;
                //    _infHeader.Medical = _infRate.Medical;
                //    _infHeader.Unemployed = _infRate.Unemployed;
                //    _infHeader.Social = _infRate.Social;

                //    //Detail:
                //    _EmployeeInfo = _wcfEmployee.Employee_GetInfo(_PDInfo.EmployeeID);
                //    _infDetail.EmployeeCode = _EmployeeInfo.Code;
                //    _infDetail.InsBookNo = _EmployeeInfo.SIBookNo;
                //    _infDetail.InsTypeCode = "TM";
                //    _infDetail.FromDate = _PDInfo.BeginDate;
                //    _infDetail.ToDate = _PDInfo.BeginDate;
                //    _infDetail.HospitalCode = _EmployeeInfo.MedicalPlace;
                //    _infDetail.MedicalRate = _infRate.Medical;
                //    _infDetail.UnemployedRate = _infRate.Unemployed;
                //    _infDetail.SocialRate = _infRate.Social;
                //    if (_infHeader.Type == "ML")
                //    {
                //        _infDetail.NewAmount = _PDInfo.BasicSalary;
                //        _infDetail.NewBasicFactor = 0;
                //        _infDetail.NewPositionFactor = 0;
                //        _infDetail.NewOverFactor = 0;
                //        _infDetail.NewJobFactor = 0;
                //        _infDetail.NewAreaFactor = 0;
                //    }
                //    else
                //    {
                //        _infDetail.NewAmount = 0;
                //        _infDetail.NewBasicFactor = _PDInfo.BasicFactor;
                //        _infDetail.NewPositionFactor = _PDInfo.PositionFactor;
                //        _infDetail.NewOverFactor = _PDInfo.OverFactor;
                //        _infDetail.NewJobFactor = _PDInfo.JobFactor;
                //        _infDetail.NewAreaFactor = _PDInfo.AreaFactor;
                //    }

                //    _infDetail.OldAmount = 0;
                //    _infDetail.OldBasicFactor = 0;
                //    _infDetail.OldPositionFactor = 0;
                //    _infDetail.OldOverFactor = 0;
                //    _infDetail.OldJobFactor = 0;
                //    _infDetail.OldAreaFactor = 0;

                //    _infDetail.RegionCode = Program.strRegionCode;
                //    _infDetail.Status = 1;
                //    _infDetail.TakeCard = false;
                //    _infDetail.TotalMonth = 0;
                //    _infDetail.Type = _infHeader.Type;

                //    if (wcfHeader.Header_CheckExist(0, Program.strBranchBHXH, _infHeader.Date, _infHeader.Month, _infHeader.Year, _infHeader.Period))
                //    {
                //        _infDetail.DocumentID = wcfHeader.Header_GetMaxBymonth(_infHeader.Month, _infHeader.Year, _infHeader.BranhCode, _infHeader.Type).ID;
                //        if (!wcfDetail.Detail_Exist(0, _infDetail.InsBookNo, _infDetail.FromDate, _infDetail.ToDate, _infDetail.InsTypeCode))
                //        {
                //            wcfDetail.Detail_Insert(_infDetail);
                //        }
                //    }
                //    else//Insert 1 bản ghi mới vào bảng Header:
                //    {
                //        _infDetail.DocumentID = wcfHeader.Header_Insert(_infHeader);
                //        wcfDetail.Detail_Insert(_infDetail);
                //    }
                //}
                #endregion

                if (_PDID > 0)
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmPositionDetailsInfo_KeyUp(object sender, KeyEventArgs e)
        {

            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void cboBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            int BranchID = 0;
            if (cboBranch.SelectedValue != null)
            {
                BranchID = Convert.ToInt32(cboBranch.SelectedValue);
                UISetting.LoadDepartment(ref cboDepartment, BranchID);
            }
        }

        private void frmPositionDetailsInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            this.DialogResult = dlgResult;
        }

        void NewSum()
        {
            decimal _sum = 0, _value1 = 0, _value2 = 0, _value3 = 0, _value4 = 0, _value5 = 0, _value6 = 0, _value7 = 0, _value8 = 0;
            _value1 = Convert.ToDecimal(txtBasicSalary.Text);
            _value2 = Convert.ToDecimal(txtPositionAllowance.Text);
            _value3 = Convert.ToDecimal(txtOverAllowance.Text);
            _value4 = Convert.ToDecimal(txtJobAllowance.Text);
            _value5 = Convert.ToDecimal(txtAreaAllowance.Text);
            _value6 = Convert.ToDecimal(txtTelephone.Text);
            _value7 = Convert.ToDecimal(txtLunch.Text);
            _value8 = Convert.ToDecimal(txtOther.Text);
            _sum = _value1 + _value2 + _value3 + _value4 + _value5 + _value6 + _value7 + _value8;
            txtTotal.Text = _sum.ToString(UIParamater.Phay2So);
        }
        void OldSum()
        {
            decimal _sum = 0, _value1 = 0, _value2 = 0, _value3 = 0, _value4 = 0, _value5 = 0, _value6 = 0, _value7 = 0, _value8 = 0;
            _value1 = Convert.ToDecimal(txtOldBasicSalary.Text);
            _value2 = Convert.ToDecimal(txtOldPositionAllowance.Text);
            _value3 = Convert.ToDecimal(txtOldOverAllowance.Text);
            _value4 = Convert.ToDecimal(txtOldJobAllowance.Text);
            _value5 = Convert.ToDecimal(txtOldAreaAllowance.Text);
            _value6 = Convert.ToDecimal(txtOldTelephone.Text);
            _value7 = Convert.ToDecimal(txtOldLunch.Text);
            _value8 = Convert.ToDecimal(txtOldOther.Text);
            _sum = _value1 + _value2 + _value3 + _value4 + _value5 + _value6 + _value7 + _value8;
            txtOldTotal.Text = _sum.ToString(UIParamater.Phay2So);
        }
        private void txtPositionAllowance_Leave(object sender, EventArgs e)
        {
            if (txtPositionAllowance.Text.Length == 0)
                txtPositionAllowance.Text = "0";
            txtPositionAllowance.Text = UISetting.GetDecimalNo(txtPositionAllowance.Text).ToString("#,##0");
            //Sum();
        }

        private void txtBasicSalary_Leave(object sender, EventArgs e)
        {
            if (txtBasicSalary.Text.Length == 0)
                txtBasicSalary.Text = "0";
            txtBasicSalary.Text = UISetting.GetDecimalNo(txtBasicSalary.Text).ToString("#,##0");
            //Sum();
        }

        private void txtOtherAllowance_Leave(object sender, EventArgs e)
        {
            if (txtOther.Text.Length == 0)
                txtOther.Text = "0";
            txtOther.Text = UISetting.GetDecimalNo(txtOther.Text).ToString("#,##0");
            //Sum();
        }

        private void txtLunchAllowance_Leave(object sender, EventArgs e)
        {
            if (txtLunch.Text.Length == 0)
                txtLunch.Text = "0";
            //txtLunch.Text = UISetting.GetDecimalNo(txtLunch.Text).ToString("#,##0");
            //Sum();
        }

        private void txtTelephoneAllowance_Leave(object sender, EventArgs e)
        {

        }
        void DataChanged(object sender, EventArgs e)
        {
            isChange = true;
        }


        //private void btnSelectGrade_Click(object sender, EventArgs e)
        //{
        //    DateTime _Date = DateTime.MinValue;
        //    _Date = UISetting.GetDate(txtDateStart.Text);
        //    frmGradeDetailList f = new frmGradeDetailList(true);
        //    if (f.ShowDialog() == DialogResult.OK)
        //    {
        //        txtBasicFactor.Text = f.Amount.ToString(UIParamater.Cham2So);
        //        txtGrade.Tag = f.GradeID;
        //        txtGrade.Text = f.GradeName;
        //        txtLevel.Tag = f.LevelID;
        //        txtLevel.Text = f.LevelName;
        //        i = f.CountMonth;
        //        if (_Date != DateTime.MinValue)
        //            txtDateSalary.Text = _Date.AddMonths(i).ToString("MM/yyyy");
        //    }
        //}

        private void btnSalaryAgreement_Click(object sender, EventArgs e)
        {
            if (_PDInfo.EmployeeID > 0)
            {
                frmPreviewReport f = new frmPreviewReport("SalaryAgreement", new string[] { _PDInfo.EmployeeID.ToString() });
                f.ShowDialog();
            }
        }

        private void txtBasicFactor_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtminSalary_TextChanged(object sender, EventArgs e)
        {
            //txtBasicSalary.Text = (UISetting.GetDecimalNo(txtBasicFactor.Text) * UISetting.GetDecimalNo(txtminSalary.Text)).ToString("#,##0");
            //Sum();
        }
        void LoadSalary()
        {
            DateTime _date = DateTime.MinValue;
            DateTime _DateRate = DateTime.MinValue;
            _date = UISetting.GetDate("01/" + txtDateStart.Text);
            string _sql = "";
            _sql = "SELECT TOP 1 BasicSalary_Amount FROM HRM_BasicSalary WHERE BasicSalary_Month<='" + _date.ToString("MM/dd/yyyy") + "' ORDER BY BasicSalary_Month DESC";
            MainServiceClient _wcfMain = Service.MainService();
            DataTable dt = _wcfMain.Query(_sql).Tables[0];
            if (dt.Rows.Count > 0)
                txtminSalary.Text = Convert.ToDecimal(dt.Rows[0]["BasicSalary_Amount"]).ToString("#,##0");
        }
        private void txtDateStart_Leave(object sender, EventArgs e)
        {
            LoadSalary();
            DateTime _Date = DateTime.MinValue;
            _Date = UISetting.GetDate(txtDateStart.Text);
            if (_Date != DateTime.MinValue)
                txtDateSalary.Text = _Date.AddMonths(i).ToString("MM/yyyy");
        }
        void Calculate()
        {
            decimal _MinSalary = 0;
            decimal _BasicFactor = 0;
            decimal _PositionFactor = 0;
            decimal _OverFactor = 0;
            decimal _JobFactor = 0;
            decimal _AreaFactor = 0;
            decimal _BasicAmount = 0;
            decimal _PositionAmount = 0;
            decimal _OverAmount = 0;
            decimal _JobAmount = 0;
            decimal _AreaAmount = 0;
            decimal _PhoneAmount = 0;
            decimal _LunchAmount = 0;
            decimal _OtherAmount = 0;
            decimal _Total = 0;
            _MinSalary = UISetting.GetDecimalNo(txtminSalary.Text);
            _BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            _PositionFactor = UISetting.GetDecimalNo(txtPositionFactor.Text);
            _OverFactor = UISetting.GetDecimalNo(txtOverFactor.Text);
            _JobFactor = UISetting.GetDecimalNo(txtJobFactor.Text);
            _AreaFactor = UISetting.GetDecimalNo(txtAreaFactor.Text);
            _BasicAmount = UISetting.GetDecimalNo(txtBasicSalary.Text);
            _PositionAmount = UISetting.GetDecimalNo(txtPositionAllowance.Text);
            _OverAmount = UISetting.GetDecimalNo(txtOverAllowance.Text);
            _JobAmount = UISetting.GetDecimalNo(txtJobAllowance.Text);
            _AreaAmount = UISetting.GetDecimalNo(txtAreaAllowance.Text);
            _PhoneAmount = UISetting.GetDecimalNo(txtTelephone.Text);
            _LunchAmount = UISetting.GetDecimalNo(txtLunch.Text);
            _OtherAmount = UISetting.GetDecimalNo(txtOther.Text);
            _Total = (_BasicFactor + _PositionFactor + _OverFactor + _JobFactor + _AreaFactor) * _MinSalary + _BasicAmount + _PositionAmount + _OverAmount + _JobAmount + _AreaAmount + _PhoneAmount + _LunchAmount + _OtherAmount;
            
            //Format so
            txtBasicSalary.Text = _BasicAmount.ToString("#,##0");
            txtPositionAllowance.Text = _PositionAmount.ToString("#,##0");
            txtOverAllowance.Text = _OverAmount.ToString("#,##0");
            txtJobAllowance.Text = _JobAmount.ToString("#,##0");
            txtAreaAllowance.Text = _AreaAmount.ToString("#,##0");
            txtTelephone.Text = _PhoneAmount.ToString("#,##0");
            txtLunch.Text = _LunchAmount.ToString("#,##0");
            txtOther.Text = _OtherAmount.ToString("#,##0");
            txtTotal.Text = _Total.ToString("#,##0");
        }

        private void txtPositionFactor_Validated(object sender, EventArgs e)
        {
            if (txtPositionFactor.Text.Length == 0)
                txtPositionFactor.Text = "0";
            Calculate();
        }

        private void txtBasicFactor_Validated(object sender, EventArgs e)
        {
            if (txtBasicFactor.Text.Length == 0)
                txtBasicFactor.Text = "0";
            Calculate();
        }

        private void txtOverFactor_Validated(object sender, EventArgs e)
        {
            if (txtOverFactor.Text.Length == 0)
                txtOverFactor.Text = "0";
            Calculate();
        }

        private void txtJobFactor_Validated(object sender, EventArgs e)
        {
            if (txtJobFactor.Text.Length == 0)
                txtJobFactor.Text = "0";
            Calculate();
        }

        private void txtAreaFactor_Validated(object sender, EventArgs e)
        {
            if (txtAreaFactor.Text.Length == 0)
                txtAreaFactor.Text = "0";
            Calculate();
        }

        private void txtBasicSalary_Validated(object sender, EventArgs e)
        {
            if (txtBasicSalary.Text.Length == 0)
                txtBasicSalary.Text = "0";
            Calculate();
        }

        private void txtPositionAllowance_Validated(object sender, EventArgs e)
        {
            if (txtPositionAllowance.Text.Length == 0)
                txtPositionAllowance.Text = "0";
            Calculate();
        }

        private void txtOverAllowance_Validated(object sender, EventArgs e)
        {
            if (txtOverAllowance.Text.Length == 0)
                txtOverAllowance.Text = "0";
            Calculate();
        }

        private void txtJobAllowance_Validated(object sender, EventArgs e)
        {
            if (txtJobAllowance.Text.Length == 0)
                txtJobAllowance.Text = "0";
            Calculate();
        }

        private void txtAreaAllowance_Validated(object sender, EventArgs e)
        {
            if (txtAreaAllowance.Text.Length == 0)
                txtAreaAllowance.Text = "0";
            Calculate();
        }

        private void txtTelephone_Validated(object sender, EventArgs e)
        {
            if (txtTelephone.Text.Length == 0)
                txtTelephone.Text = "0";
            Calculate();
        }

        private void txtLunch_Validated(object sender, EventArgs e)
        {
            if (txtLunch.Text.Length == 0)
                txtLunch.Text = "0";
            Calculate();
        }

        private void txtOther_Validated(object sender, EventArgs e)
        {
            if (txtOther.Text.Length == 0)
                txtOther.Text = "0";
            Calculate();
        }

        private void cboGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboGrade.SelectedValue != null)
            {
                _GradeID = Convert.ToInt32(cboGrade.SelectedValue);
                UISetting.LoadLevel(ref cboLevel, _GradeID);
            }
        }

        private void cboLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboLevel.SelectedValue != null)
            {
                _LevelID = Convert.ToInt32(cboLevel.SelectedValue);
                CountMonth(_GradeID, _LevelID);
            }
        }

        private void txtDateStart_Validated(object sender, EventArgs e)
        {
            _GradeID = Convert.ToInt32(cboGrade.SelectedValue);
            _LevelID = Convert.ToInt32(cboLevel.SelectedValue);
            CountMonth(_GradeID, _LevelID);
        }
        void CountMonth(int Grade, int Level)
        {
            DataTable dtDetail = _wcfGradeDetail.GradeDetail_GetAmount(Grade, Level).Tables[0];
            if (dtDetail.Rows.Count > 0)
            {
                i = Convert.ToInt32(dtDetail.Rows[0]["GradeDetail_Month"]);
                if (Convert.ToDecimal(dtDetail.Rows[0]["GradeDetail_Amount"]) < 20)
                    txtBasicFactor.Text = Convert.ToDecimal(dtDetail.Rows[0]["GradeDetail_Amount"]).ToString("#0.0#");
                else
                    txtBasicSalary.Text = Convert.ToDecimal(dtDetail.Rows[0]["GradeDetail_Amount"]).ToString("#,##0");
            }
            else
            {
                i = 0;
                txtBasicFactor.Text = "0";
                txtBasicSalary.Text = "0";
            }
            DateTime _Date = DateTime.MinValue;
            _Date = UISetting.GetDate(txtDateStart.Text);
            if (_Date != DateTime.MinValue)
                txtDateSalary.Text = _Date.AddMonths(i).ToString("MM/yyyy");

        }

        private void txtDateEnd_Validated(object sender, EventArgs e)
        {
            if (txtDateEnd.Text != "")
                txtDateSalary.Text = txtDateEnd.Text;
        }

        private void txtOverFactor_Percent_Validated(object sender, EventArgs e)
        {
            if (txtOverFactor.Text.Length == 0)
                txtOverFactor.Text = "0";
            //
            decimal BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            decimal Percent_OverFactor = UISetting.GetDecimalNo(txtOverFactor_Percent.Text);
            txtOverFactor.Text = ((Percent_OverFactor * BasicFactor) / 100).ToString(UIParamater.Cham3So);
            
            //
            Calculate();
        }

        private void txtJobFactor_Percent_Validated(object sender, EventArgs e)
        {
            if (txtJobFactor_Percent.Text.Length == 0)
                txtJobFactor_Percent.Text = "0";
            //
            decimal BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            decimal PositionFactor = UISetting.GetDecimalNo(txtPositionFactor.Text);
            decimal Percent_JobFactor = UISetting.GetDecimalNo(txtJobFactor_Percent.Text);
            decimal OverFactor = UISetting.GetDecimalNo(txtOverFactor.Text);
            decimal JobFactor = (BasicFactor + PositionFactor + OverFactor) * Percent_JobFactor / 100;

            //
            txtOverFactor.Text = OverFactor.ToString(UIParamater.Cham3So);
            txtJobFactor.Text = JobFactor.ToString(UIParamater.Cham3So);
            //
            Calculate();
        }

    }
}
